#!/usr/bin/env python
#-*- coding:UTF-8 -*-

import os,shutil,sys
import platform
separator = '/'
sysstr = platform.system()
if(platform.system() =="Windows"):
    separator = '\\'
folder = input('请输入需要转码的文件夹路径:')

def traverse(f):
    fs = os.listdir(f)
    for f1 in fs:
        src_path = os.path.join(f,f1)
        if not os.path.isdir(src_path):
            if file_extension(src_path) == '.mp4' or file_extension(src_path) == '.MP4':
               format_to_mp4(src_path)
        else:
            traverse(src_path)

def format_to_mp4(srcfile):
    dstfile = os.path.splitext(srcfile)[0] + 'old.mp4'
    shutil.move(srcfile,dstfile)
    if(platform.system() =="Windows"):
        comm = 'ffmpeg -i "{0}" -strict -2 "{1}"'.format(dstfile, srcfile)
        os.system(comm)
    else:
        comm = 'ffmpeg -i {0} -strict -2 {1}'.format(dstfile, srcfile)
        os.system(comm)
    os.remove(dstfile)

# 获取文件的扩展名
def file_extension(path):
    splits = os.path.splitext(path)
    return splits[1]

if __name__ == '__main__':
    traverse(folder)



